package com.aaa.rabbitmq.one;

import com.aaa.rabbitmq.one.util.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 生产者
 * zhc
 */
public class Producer {

    //队列名称
    public static final String QUEUE_NAME="helloTest";


    //发消息
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtil.getChannel();
        //参数
        Map<String, Object> arguments=new HashMap<>(1);
        arguments.put("x-max-priority",10);//官方允许优先级范围 0-255  此处设置为10 不宜过大浪费cpu和内存

        //创建队列
        channel.queueDeclare(QUEUE_NAME,false,false,false,arguments);

        for (int i = 1; i < 11; i++) {
            String msg="info"+i;
            if (i==5){
                AMQP.BasicProperties properties =new AMQP.BasicProperties().builder().priority(5).build();
                channel.basicPublish("",QUEUE_NAME,properties,msg.getBytes(StandardCharsets.UTF_8));
            }else {
                channel.basicPublish("",QUEUE_NAME,null,msg.getBytes(StandardCharsets.UTF_8));
            }

        }

    }

}
